<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\model\Admin;
use Validator;
use Illuminate\Support\Facades\Cookie;

class AdminController extends Controller
{
    /**
     * 后台首页
     */
    public function login()
    {
        return view('admin.site.login');
    }

    /*
    * 后台用户验证
    */
    public function verifys(Request $request)
    {
    	$all_get = $request->all();

		$admin = new Admin;
		$is_user = $admin->VerifyUser($all_get['username']);

		if (empty($is_user))
		{
			return $this->back_json('400','ERROR','用户名不存在');
		} else {
			if ($is_user['password'] !== md5($all_get['password']))
			{
				return $this->back_json('401','ERROR','密码错误');
			} else {
				$token = array(
					'id' => $is_user['id'],
					'phone' => $is_user['phone'],
					'name' => $is_user['admin_name'],
					'token' => md5($is_user['id'].$is_user['phone'])
				);
				$status = array(
                            'code' => '200',
                            'status' => 'SUCCESS',
                            'msg'   => '登陆成功',
                             );
				$username = $is_user['admin_name'] ? $is_user['admin_name'] : $is_user['phone'];
				return response()->json($status)->cookie("adminlogin", serialize($token),  24*60);
			}
		}
    }

    /*
     * 登录成功后跳入后台首页
     */
    public function index(Request $request)
    {
    	$label = '希望寻人后台管理';
		return view('admin.site.index',compact('label'));
    }

    //用户退出
	public function cookieClear()
	{
		setcookie('adminlogin','',-1,'/');
		setcookie('adminname','',-1,'/');
		return view('admin.site.login');
	}

	//修改密码
	public function pwdUpdate(Request $request)
	{
		$label = '用户管理-修改密码';

		if($request->ajax())
		{
			$validator = Validator::make($request->all(), [
            	'pwd1' => 'required',
            	'pwd2' => 'required',
            	'pwd3' => 'required',
        	]);

			if ($validator->fails())
			{
        	   	return $this->back_json('400','ERROR','请检查信息是否填写完整');
        	}
	
			$pwd1 = $request->pwd1;
			$pwd2 = $request->pwd2;
			$pwd3 = $request->pwd3;
	
			if ($pwd2 != $pwd3)
			{
				return $this->back_json('400','ERROR','两次密码不一致');
			}
			$user_data = $request->cookie('adminlogin');
        	$user_data = unserialize($user_data);

			$admin = new Admin;
			$oneuser = $admin->findID($user_data['id']);

			if (md5($pwd1) !== $oneuser['password'])
			{
				return $this->back_json('401','ERROR','初始密码错误');
			}

			$updatePwd = Admin::where('id',$user_data['id'])->update(['password' => md5($pwd2)]);
		
			return $this->call_back_status($updatePwd);
		}

		return view('admin.site.pwdupdate',compact('label'));
	}

	//用户列表
	public function adminUser(Request $request)
	{
		$label = '后台用户管理';
		if ($request->ajax()) 
		{
			//后台用户列表接口
			$page = $request->page ? $request->page : 1;
			$limit = $request->limit ? $request->limit : 10;
			$offset = ($page - 1) * $limit;
			$count = Admin::count('id');
	
			$adminmsg = Admin::limit($limit)->offset($offset)->get();

			if (!empty($adminmsg)) {
				foreach ($adminmsg as $key => &$value) {
					$value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
					unset($value);
				}
			}
			return $this->return_ajax_data($adminmsg,'','',$count);
		}
		return view('admin.site.adminuser',compact('label'));
	}

	//修改用户
	public function edituser(Request $request)
	{
		$all = $request->all();
		$updateUser = Admin::where('id',$all['id'])->update(['admin_name' => $all['admin_name'],'sex' => $all['sex']]);
		return $this->call_back_status($updateUser);
	}

	//删除用户
	public function deleteUser(Request $request)
	{
		$id = $request->id;
		$deleteUser = Admin::where('id','=',$id)->delete();
		return $this->call_back_status($deleteUser);
	}

	//添加用户
	public function adduser(Request $request)
	{
		$all = $request->all();
		$phone_only = Admin::where('phone','=',$all['phone'])->get()->toArray();
		if(empty($phone_only))
		{
			$save_user = new Admin;
			$save_user->admin_name = $all['admin_name'];
			$save_user->phone = $all['phone'];
			$save_user->password = md5($all['password']);
			$save_user->sex = $all['sex'];
			$save_user->create_time = time();
			if($save_user->save())
			{
				return $this->call_back_status($save_user);
			}
		}
		else
		{
			return json_encode(array(
                'code' => '401',
                'status' => 'ERROR',
                'msg'   => '该手机已注册',
                 ));
		}
		
	}
}